package com.jirong.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jirong.shop.mapper.TSeckillOrderMapper;
import com.jirong.shop.pojo.TSeckillOrder;
import com.jirong.shop.pojo.TUser;
import com.jirong.shop.service.TSeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jirong
 * @since 2022-01-16
 */
@Service
public class TSeckillOrderServiceImpl extends ServiceImpl<TSeckillOrderMapper, TSeckillOrder> implements TSeckillOrderService {
    @Autowired
    private TSeckillOrderMapper seckillOrderMapper;
    @Autowired
    @Qualifier("redisTemplate1")
    private RedisTemplate redisTemplate;

    @Override
    public Long getResult(TUser user, Long goodsId) {
        TSeckillOrder seckillOrder = seckillOrderMapper.selectOne(new
                QueryWrapper<TSeckillOrder>().eq("user_id", user.getId()).eq("goods_id", goodsId));
        if (null != seckillOrder) {
//            返回商品的订单才对，不是秒杀表的id
            return seckillOrder.getOrderId();
        } else {
            if (redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
                return -1L;
            } else {
                return 0L;
            }
        }
    }
}
